package com.wandian.notepad.mvvm.notepadadd;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.BottomRecyclePopupWindow;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.DialogUtil;
import com.mylib.libcore.utils.TimeUtils;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.bean.NoteTypeBean;
import com.wandian.notepad.databinding.NotepadActivityAddBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 添加记事本activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NOTEPAD_ADD)
public class NotepadAddActivity extends BaseMVVMActivity<NotepadAddActivityViewModel> implements BottomRecyclePopupWindow.ItemClink {
    private NotepadActivityAddBinding dataBinding;
    private ArrayList<String> listNotepadType = new ArrayList<>();
    private String typeId ="";

    private int reMind = 0; //0关闭，1开启
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.notepad_activity_add);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        mViewModel.notepadTypeList();
    }


    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText("添加记事");
        dataBinding.tvData.setText(TimeUtils.stampToDateClock(System.currentTimeMillis()+""));

        mViewModel.noteTypeList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean)){
                for(NoteTypeBean bean:listBean){
                    listNotepadType.add(bean.getType());
                }
            }
        });
    }


    private void initClink(){
        dataBinding.bindSysTwoToolBar.toolBarIvBack.setOnClickListener(v -> finish());

        dataBinding.btnSave.setOnClickListener(v -> {
            finishhideSoftBoard();
            if(UIUtils.isEmpty(dataBinding.etTitle.getText())){
                ToastUtil.show("请填写备忘标题");
                return;
            }
            if(UIUtils.isEmpty(dataBinding.tvData.getText())){
                ToastUtil.show("请设定备忘时间");
                return;
            }
            if(UIUtils.isEmpty(dataBinding.tvType.getText())){
                ToastUtil.show("请选择备忘类型");
                return;
            }
            if(UIUtils.isEmpty(dataBinding.etContent.getText())){
                ToastUtil.show("请填写备忘内容");
                return;
            }
            String reMindTime = System.currentTimeMillis()+"";
            if(reMind==1){
                if(UIUtils.isEmpty(dataBinding.tvNotesRemindTime.getText())){
                    ToastUtil.show("请设置闹钟提醒时间");
                    return;
                }else {
                    reMindTime = dataBinding.tvNotesRemindTime.getText().toString();
                }
            }
            if(UIUtils.isEmpty(typeId)) typeId = "1";

            mViewModel.addNotepad(dataBinding.etContent.getText().toString().trim(),reMind+"",reMindTime,
                    dataBinding.tvData.getText().toString()
                    ,dataBinding.etTitle.getText().toString().trim(),typeId);
        });

        //类型
        dataBinding.rlType.setOnClickListener(v ->  {
            finishhideSoftBoard();
            DialogUtil.showCommonBottomRecycleDialog(dataBinding.llTop,this::itemTextClink,"NotepadType",listNotepadType);
        });
        //日期
        dataBinding.rlData.setOnClickListener(v -> {
            finishhideSoftBoard();
            UIUtils.showHourTimeDialog(this,dataBinding.tvData);
        });
        //闹钟
        dataBinding.rlTixingTime.setOnClickListener(v ->{
            finishhideSoftBoard();
            UIUtils.showClockDialog(this,dataBinding.tvNotesRemindTime);
        });

        //提醒
        dataBinding.tvNotesRemind.setOnClickListener(v ->{
            finishhideSoftBoard();
            if(reMind==0){
                reMind = 1;
                dataBinding.rlTixingTime.setVisibility(View.VISIBLE);
                dataBinding.tvNotesRemind.setText("开启");
            }else {
                reMind = 0;
                dataBinding.rlTixingTime.setVisibility(View.GONE);
                dataBinding.tvNotesRemind.setText("关闭");
            }
        });
    }



    @Override
    protected void onDestroy() {
        finishhideSoftBoard();
        super.onDestroy();
    }

    @Override
    public void itemTextClink(int index, String text, String id, String type) {
        if(!UIUtils.isEmpty(text)){
            dataBinding.tvType.setText(text+"");
            if(mViewModel.noteTypeList.getValue() != null && mViewModel.noteTypeList.getValue().size()>0){
                for(NoteTypeBean bean : mViewModel.noteTypeList.getValue()){
                    if(!UIUtils.isEmpty(bean.getType()) && bean.getType().equals(text)){
                        typeId = bean.getId()+"";
                        return;
                    }
                }
            }
        }
    }
}
